package com.zyiot.lbs;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.umeng.analytics.MobclickAgent;
import com.zyiot.adapter.EshopAdapter;
import com.zyiot.listview.EshopListview;
import com.zyiot.listview.EshopListview.IXListViewListener;
import com.zyiot.model.Eshop;

public class TabListActivity extends Activity implements IXListViewListener {
	private Context context;
	private boolean initSearchFlag = false;

	private RelativeLayout progress;
	private EshopAdapter adapter;
	private Handler mHandler;
	private EshopListview listView;
	private int start = 0;
	private static int refreshCnt = 0;
	List<Eshop> list;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_eshop_list);
		InitApplication app = new InitApplication();
		// ListView listView = (ListView) findViewById(R.id.list_eshop_View);
		listView = (EshopListview) findViewById(R.id.list_eshop_View);
		listView.setPullLoadEnable(true);
		Intent itlist = getIntent();

		list = new ArrayList<Eshop>();
		Object[] objs = (Object[]) itlist.getSerializableExtra("list");
		for (int i = 0; i < objs.length; ++i) {
			list.add((Eshop) objs[i]);
		}
		
		
		//adapter = new ContentAdapter(this, list);

		if (adapter != null) {
			listView.setAdapter(adapter);
			listView.setItemsCanFocus(false);
			listView.setOnScrollListener(mScrollListener);
		}
		listView.setXListViewListener(this);
		Button btnMap = (Button) findViewById(R.id.btnmap);
		btnMap.setOnClickListener(listener);
		mHandler = new Handler();

	}

	// 按钮监听
	private OnClickListener listener = new OnClickListener() {
		@Override
		public void onClick(View arg0) {
			finish();
		}
	};
	OnScrollListener mScrollListener = new OnScrollListener() {

		@Override
		public void onScrollStateChanged(AbsListView view, int scrollState) {
			switch (scrollState) {
			case OnScrollListener.SCROLL_STATE_FLING:
				adapter.setFlagBusy(true);
				break;
			case OnScrollListener.SCROLL_STATE_IDLE:
				adapter.setFlagBusy(false);
				break;
			case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
				adapter.setFlagBusy(false);
				break;
			default:
				break;
			}
			adapter.notifyDataSetChanged();
		}

		@Override
		public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

		}
	};

	private void onLoad() {
		listView.stopLoadMore();
	}

	@Override
	public void onLoadMore() {
		mHandler.postDelayed(new Runnable() {
			@Override
			public void run() {

				adapter.notifyDataSetChanged();
				onLoad();
			}
		}, 2000);
	}
	@Override
	public void onResume() {
	    super.onResume();
	    MobclickAgent.onResume(this);
	}
	@Override
	public void onPause() {
	    super.onPause();
	    MobclickAgent.onPause(this);
	}
}
